if not PQR_LoadedDataFile then
	PQR_LoadedDateFile = 1
	print("|cffFF6EB4--Failroad PvP data file--|cffffffff")
elseif PQR_LoadedDataFile then
	print("|cffFF6EB4--Reloading Data--|cffffffff")
end


--CastSpell--
function _castSpell(spellid,tar)
	if UnitCastingInfo("player") == nil
	and UnitChannelInfo("player") == nil
	and IsPlayerSpell(spellid) == true
	and cdRemains(spellid) == 0
	then
		if tar ~= nil
		and rangeCheck(spellid,tar) == nil
			then
			return false
		elseif tar ~= nil
		and rangeCheck(spellid,tar) == true
			then
			CastSpellByName(GetSpellInfo(spellid),tar)
			return true
		elseif tar == nil
			then
			CastSpellByName(GetSpellInfo(spellid))
			return true
		else
	return false
	end
end
end

--Combat and Melee Check--
function inMelee()
	if UnitAffectingCombat("player") ~= nil
	and IsSpellInRange(GetSpellInfo(78),"target") == 1
	and UnitExists("target") ~= nil
	and UnitCanAttack("player","target") ~= nil
	then return true
	end
end

--Combat Check--
function inCombat()
if UnitAffectingCombat("player") ~= nil
	then
		return true
	end
end

--Get HP simple--
function getHp(unit)
if UnitExists(unit) ~= nil
	then
		return 100 * UnitHealth(unit) / UnitHealthMax(unit)
	end
end

--Range Check Simple--
function rangeCheck(spellid,unit)
if IsSpellInRange(GetSpellInfo(spellid),unit) == 1
then
	return true
end
end


--Amount of Cooldown that remains--
function cdRemains(spellid)
	if select(2,GetSpellCooldown(spellid)) + (select(1,GetSpellCooldown(spellid)) - GetTime()) > 0
		then return select(2,GetSpellCooldown(spellid)) + (select(1,GetSpellCooldown(spellid)) - GetTime())
	else return 0
	end
end




--Buff/Debuff/CC/Reflect functions--

--Function Check Snares--
local PvPslows 			= {
	45524,		-- Chains of Ice
	50435,		-- Chilblains
	115000,		-- Remorseless Winter
	50259,		-- Dazed 
	58180,		-- Infected Wounds
	61391,		-- Typhoon
	127797,		-- Ursol's Vortex
	82941,		-- Ice Trap (Hunter)
	135299,		-- IceTrap Debuff
	35101,		-- Concussive Barrage
	5116,		-- Concussive Shot
	61394,		-- Frozen Wake 
	13810,		-- Ice Trap
	50433,		-- Ankle Crack 
	54644,		-- Frost Breath 
	121288,		-- Chilled 
	120,		-- Cone of Cold
	116,		-- Frostbolt
	44614,		-- Frostfire Bolt
	113092,		-- Frost Bomb
	31589,		-- Slow
	116095,		-- Disable
	118585,		-- Leer of the Ox
	123727,		-- Dizzying Haze
	123586,		-- Flying Serpent Kick
	110300,		-- Burden of Guilt
	63529,		-- Dazed - Avenger's Shield
	20170,		-- Seal of Justice
	15407,		-- Mind Flay
	3409,		-- Crippling Poison
	26679,		-- Deadly Throw
	119696,		-- Debilitation
	3600,		-- Earthbind 
	77478,		-- Earthquake 
	8034,		-- Frostbrand Attack
	8056,		-- Frost Shock
	51490,		-- Thunderstorm
	18223,		-- Curse of Exhaustion
	47960,		-- Shadowflame
	1715,		-- Hamstring
	12323,		-- Piercing Howl
	1604		-- Dazed 

				}
function isSlowed(unit)
for i=1,#PvPslows do
	if UnitDebuffID(unit,PvPslows[i]) ~= nil
		then
			return true
		end
	end
end


--Function Check Slow Immunes--
local immuneToSlowID 	= {
					-- DEATH KNIGHT
					-- DRUID
					-- HUNTER (Master's Call / Bestial Wrath)
						54216,
						19574,
					-- MAGE (Ice Block)
						45438,
					-- MONK
					-- PALADIN (Hand of Freedom / Hand of Protection / Divine Shield)
						1044,
						1022,
						642,
					-- PRIEST (Dispersion)
						47585,
					-- ROGUE
					-- SHAMAN (Windwalk Totem)
						114896,
					-- WARLOCK
					-- WARRIOR (Bladestorm)
						46924
				}
function immuneToSlow(unit)
for i=1,#immuneToSlowID do
	if UnitBuffID(unit,immuneToSlowID[i]) ~= nil
		then
			return true
		end
	end
end


--Function SpellReflect--
local reflectCC 		= { 
					-- DEATH KNIGHT
					-- DRUID
						33786,		-- Cyclone
					-- HUNTER
					-- MAGE
						28272,		-- Pig
						118,		-- Sheep
						61305,		-- Cat
						61721,		-- Rabbit
						61780,		-- Turkey
						28271,		-- Turtle
						113092,		-- Frost Bomb
					-- MONK
					-- PALADIN
						20066,		-- Repentance
					-- PRIEST
						605,		-- Mind Control
					-- ROGUE
					-- SHAMAN
						51514,		-- Hex
					-- WARLOCK
						5782,		-- Fear
						118699		-- Fear
					-- WARRIOR
}
local reflectDMG		= {
						-- DEATH KNIGHT
						-- DRUID
							78674,		-- Starsurge
						-- HUNTER
						-- MAGE
							113092,		-- Frost Bomb
							30451,		-- Arcane Blast
						-- MONK
						-- PALADIN
						-- PRIEST
							8092,		-- Mind Blast
						-- ROGUE
						-- SHAMAN
							51505,		-- Lava Burst
							117014,		-- Elemental Blast
						-- WARLOCK
							116858,		-- Chaos Bolt
							6353		-- Soul Fire
						-- WARRIOR
						}
function spellReflect(unit)
	if UnitCastingInfo(unit) ~= nil
	and UnitIsEnemy("player", unit) ~= nil
	and IsSpellInRange(GetSpellInfo(57755), unit) == 1
	and select(2,GetSpellCooldown(23920)) == 0
	and UnitBuffID("player",23920) == nil
	then
		for i=1, #reflectCC do
			if UnitCastingInfo(unit) == GetSpellInfo(reflectCC[i])
				then
				local _, _, _, _,  startTimer, endTimer = UnitCastingInfo(unit)
				local timeSinceStart = (GetTime() * 1000 - startTimer) / 1000
				local castTime = endTimer - startTimer
				local currentPercent = timeSinceStart / castTime * 100000
					if currentPercent > 45
						then
								return true
						end
				end
		end
		for i=1, #reflectDMG do
			if UnitCastingInfo(unit) == GetSpellInfo(reflectDMG[i])
			and UnitIsUnit("player",unit.."target")
				then
				local _, _, _, _,  startTimer, endTimer = UnitCastingInfo(unit)
				local timeSinceStart = (GetTime() * 1000 - startTimer) / 1000
				local castTime = endTimer - startTimer
				local currentPercent = timeSinceStart / castTime * 100000
					if currentPercent > 45
					and getHp("player") >= 85
						then
								return true
						end
				end
		end
	end
end


--Function Check Immunities--
local immuneID			= {
						110700, --Divine Shield
						1022, 	--Hand of Protection
						45438, 	--Ice Block
						31224,	--Cloak of Shadows
						19263, 	--Deterrence
						97417	--BrittleBarrier
}
function isImmune(unit)
	for i=1,#immuneID do
		if UnitBuffID(unit,immuneID[i]) ~= nil
			then
				return true
			end
		end
end


--Function Check Attack Power Buff--
local apBuff 			= {
							57330,	--Horn of Winter
							19506	--Trueshot Aura
							}
function hasApBuff()
for i=1,#apBuff do
	if UnitBuffID("player",apBuff[i]) ~= nil
		then
			return true
		end
	end
end

--Function Check if unit is CC'ed--
local ccIDs 			= { 
	108194,		-- Asphyxiate
	115001,		-- Remorseless Winter
	91800,		-- Gnaw
	91797,		-- Monstrous Blow (Dark Transformation)
	102795,		-- Bear Hug
	33786,		-- Cyclone
	99,			-- Disorienting Roar
	2637,		-- Hibernate
	22570,		-- Maim
	5211,		-- Mighty Bash
	9005,		-- Pounce
	110698,		-- Hammer of Justice 
	113004,		-- Intimidating Roar 
	113056,		-- Intimidating Roar 
	117526,		-- Binding Shot
	3355,		-- Freezing Trap
	1513,		-- Scare Beast
	19503,		-- Scatter Shot
	19386,		-- Wyvern Sting
	90337,		-- Bad Manner 
	24394,		-- Intimidation
	50519,		-- Sonic Blast 
	56626,		-- Sting 
	118271,		-- Combustion Impact
	44572,		-- Deep Freeze
	31661,		-- Dragon's Breath
	118,		-- Polymorph
	61305,		-- Polymorph: Black Cat
	28272,		-- Polymorph: Pig
	61721,		-- Polymorph: Rabbit
	61780,		-- Polymorph: Turkey
	28271,		-- Polymorph: Turtle
	82691,		-- Ring of Frost
	123393,		-- Breath of Fire 
	126451,		-- Clash
	122242,		-- Clash 
	119392,		-- Charging Ox Wave
	119381,		-- Leg Sweep
	115078,		-- Paralysis
	105421,		-- Blinding Light
	115752,		-- Blinding Light 
	105593,		-- Fist of Justice
	853,		-- Hammer of Justice
	119072,		-- Holy Wrath
	20066,		-- Repentance
	10326,		-- Turn Evil
	113506,		-- Cyclone 
	605,		-- Dominate Mind
	64044,		-- Psychic Horror
	8122,		-- Psychic Scream
	113792,		-- Psychic Terror 
	9484,		-- Shackle Undead
	87204,		-- Sin and Punishment
	2094,		-- Blind
	1833,		-- Cheap Shot
	1776,		-- Gouge
	408,		-- Kidney Shot
	113953,		-- Paralysis 
	6770,		-- Sap
	76780,		-- Bind Elemental
	77505,		-- Earthquake
	51514,		-- Hex
	118905,		-- Static Charge 
	118345,		-- Pulverize
	710,		-- Banish
	54786,		-- Demonic Leap
	5782,		-- Fear 
	118699,		-- Fear
	5484,		-- Howl of Terror
	6789,		-- Mortal Coil
	30283,		-- Shadowfury
	104045,		-- Sleep 
	89766,		-- Axe Toss 
	115268,		-- Mesmerize 
	6358,		-- Seduction 
	7922,		-- Charge Stun
	118895,		-- Dragon Roar
	5246,		-- Intimidating Shout 
	20511,		-- Intimidating Shout 
	132168,		-- Shockwave
	105771,		-- Warbringer
	107079,		-- Quaking Palm
	13327,		-- Reckless Charge
	20549		-- War Stomp
}
function hasCC(unit)
for i=1,#ccIDs do
	if UnitDebuffID(unit,ccIDs[i]) ~= nil
		then
			return true
		end
	end
end					

--Function check if rooted--
local roots				= {
						96294,	--Chains Of Ice
						91807,	--ShamblingRush
						339,		-- Entangling Roots
						19975,		-- Entangling Roots (Nature's Grasp)
						45334,		-- Immobilized (Wild Charge - Bear)
						102359,		-- Mass Entanglement
						110693,		-- Frost Nova (Mage)
						19185,		-- Entrapment
						128405,		-- Narrow Escape
						50245,		-- Pin (Crab)
						54706,		-- Venom Web Spray (Silithid)
						4167,		-- Web (Spider)
						122,		-- Frost Nova
						111340,		-- Ice Ward
						116706,		-- Disable
						113275,		-- Entangling Roots (Symbiosis)
						123407,		-- Spinning Fire Bloss
						113275,		-- Entangling Roots (Symbiosis)
						87194,		-- Glyph of Mind Blast
						114404,		-- Void Tendril's Grasp
						115197,		-- Partial Paralysis
						64695,		-- Earthgrab (Earthgrab Totem)
						63685,		-- Freeze (Frozen Power)
						107566,		-- Staggering Shout
						39965,		-- Frost Grenade
						55536,		-- Frostweave Net
						13099		-- Net-o-Matic
}
function hasRoot(unit)
for i=1,#roots do
	if UnitDebuffID(unit,roots[i]) ~= nil
		then
			return true
		end
	end
end

--Function check if used warrior CD's--
local myBigCDs 			= { 12292, --BloodBath
						   	1719, --Reck
							114206, --SkullBanner
							126679, --PvP Trinket
							107574 --Avatar
				}
function poppedCDs(unit)
for i=1,#myBigCDs do
	if UnitBuffID(unit,myBigCDs[i]) ~= nil
		then
			return true
		end
	end
end


--Custom Target Tables--							
arenaTars				= { "arena1",
							"arena2",
							"arena3",
							"arena4",
							"arena5"
							}
custTars 				= {"target",
							"focus",
							"arena1",
							"arena2",
							"arena3",
							"arena4",
							"arena5"
						 }					 
totemTars 				= {"Spirit Link Totem",
						"Healing Tide Totem",
						"Mana Tide Totem",
						"Capacitor Totem",
						"Earthgrab Totem",
						"Earthbind Totem",
						"Stone Bulwark Totem"
						}
						
---GroupInfo--
function GroupInfo()
  members, group = { { Unit = "player", HP = CalculateHP("player"), UTT = "playertarget" } }, { low = 0 } 
  group.type = IsInRaid() and "raid" or "party" 
  group.number = GetNumGroupMembers()
  for i=1,group.number do
  if CanHeal(group.type..i)
  then 
    local unit, hp, utt = group.type..i, CalculateHP(group.type..i), group.type..i.."target" 
    table.insert( members,{ Unit = unit, HP = hp , UTT = utt} ) 
    if hp < 98
    then
    	group.low = group.low + 1 
    end 
  end
  end 
  if group.type == "raid"
  and #members > 1
  then 
 	table.remove(members,1) 
  end 
  table.sort(members, function(x,y) return x.HP < y.HP end)
  local customtarget = CanHeal("target") and "target" -- or CanHeal("mouseover") and GetMouseFocus() ~= WorldFrame and "mouseover" 
  	if customtarget
  	then
	  table.sort(members, function(x) return UnitIsUnit(customtarget,x.Unit) end) 
	  table.sort(members, function(x) return UnitCanAttack("player",x.UTT) end)
	end 
end

function CanHeal(t)
  if UnitCanCooperate("player",t)
  and not UnitIsEnemy("player",t) 
  and not UnitIsCharmed(t)
  and not UnitIsDeadOrGhost(t)
  and not PQR_IsOutOfSight(t,1)
  and not UnitDebuffID(t,33786) 
  and not UnitDebuffID(t,122370)
  then
  	return true
  end 
end

function CalculateHP(t)
  incomingheals = UnitGetIncomingHeals(t) or 0
  return 100 * ( UnitHealth(t) ) / UnitHealthMax(t)
end
---EndGroupInfo


---------------------------------
-- Debug & Notification Frame
---------------------------------
-- Update Debug Frame
bgreen_NotifyFrame = nil
function bgreen_NotifyFrame_OnUpdate()
 if (bgreen_NotifyFrameTime < GetTime() - 5) then
  local alpha = bgreen_NotifyFrame:GetAlpha()
  if (alpha ~= 0) then bgreen_NotifyFrame:SetAlpha(alpha - .02) end
  if (alpha == 0) then bgreen_NotifyFrame:Hide() end
 end
end

-- Debug messages.
function bgreen_Notify(message)
 bgreen_NotifyFrame.text:SetText(message)
 bgreen_NotifyFrame:SetAlpha(1)
 bgreen_NotifyFrame:Show()
 bgreen_NotifyFrameTime = GetTime()
end

-- Debug Notification Frame
bgreen_NotifyFrame = CreateFrame('Frame')
bgreen_NotifyFrame:ClearAllPoints()
bgreen_NotifyFrame:SetHeight(300)
bgreen_NotifyFrame:SetWidth(300)
bgreen_NotifyFrame:SetScript('OnUpdate', bgreen_NotifyFrame_OnUpdate)
bgreen_NotifyFrame:Hide()
bgreen_NotifyFrame.text = bgreen_NotifyFrame:CreateFontString(nil, 'BACKGROUND', 'PVPInfoTextFont')
bgreen_NotifyFrame.text:SetAllPoints()
bgreen_NotifyFrame:SetPoint('CENTER', 0, 200)
bgreen_NotifyFrameTime = 0